package com.itcast.ixun.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itcast.ixun.R;
import com.itcast.ixun.models.HomeModels;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by lanouhn on 16/9/24.
 */
public class ItemAdapter extends RecyclerView.Adapter {
    private List<HomeModels.DataBean.ListBean> lists;
    private Context context;

    public ItemAdapter(List<HomeModels.DataBean.ListBean> lists, Context context) {
        this.lists = lists;
        this.context = context;
    }

    public ItemAdapter(List<HomeModels.DataBean.ListBean> lists) {
        this.lists = lists;
    }

    @Override
    public int getItemViewType(int position) {
        return (position + 1) % 5;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == 1) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_home_hot_one, null);
            holder = new ViewHolderFrist(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_home_hot_two, null);
            holder = new ViewHolderSecond(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = (position + 1) % 5;
        if (type == 1) {
            ViewHolderFrist holderFrist = (ViewHolderFrist) holder;
            holderFrist.itemHomeTopDescription.setText(lists.get(position).getTitle());
            Picasso.with(context).load(lists.get(position).getImg_src()).into(((ViewHolderFrist) holder).itemHomeImageBig);
        } else {
            ViewHolderSecond holderSecond = (ViewHolderSecond) holder;
            holderSecond.itemHomeImageDescription.setText(lists.get(position).getTitle());
            Picasso.with(context).load(lists.get(position).getImg_src()).into(((ViewHolderSecond) holder).itemHomeImage);
            holderSecond.itemType.setText(lists.get(position).getCate_title());
            holderSecond.readNum.setText(lists.get(position).getVisit_num() + "");

        }
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    class ViewHolderFrist extends RecyclerView.ViewHolder {
        private ImageView itemHomeImageBig;
        private TextView itemHomeTopDescription;

        public ViewHolderFrist(View itemView) {
            super(itemView);
            itemHomeImageBig = (ImageView) itemView.findViewById(R.id.item_home_image_big);
            itemHomeTopDescription = (TextView) itemView.findViewById(R.id.item_home_top_description);
        }
    }

    class ViewHolderSecond extends RecyclerView.ViewHolder {
        private TextView itemHomeImageDescription;
        private TextView itemType;
        private ImageView itemHomeImage;
        private TextView readNum;

        public ViewHolderSecond(View itemView) {
            super(itemView);
            itemHomeImageDescription = (TextView) itemView.findViewById(R.id.item_home_image_description);
            itemType = (TextView) itemView.findViewById(R.id.item_type);
            itemHomeImage = (ImageView) itemView.findViewById(R.id.item_home_image);
            readNum = (TextView) itemView.findViewById(R.id.read_num);
        }
    }

}
